package cn.zr.aitool;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import cn.zr.aitool.activitys.HouseDetailActivity;
import cn.zr.aitool.activitys.ItemDetailActivity;
import cn.zr.aitool.activitys.JiJinDetailActivity;
import cn.zr.aitool.activitys.P2pDetailActivity;
import cn.zr.aitool.bean.NavText;
import cn.zr.aitool.db.ContentColumn;
import cn.zr.aitool.db.ContentManager;
import cn.zr.aitool.db.DataBaseHelper;
import cn.zr.aitool.db.NavTextManager;
import cn.zr.aitool.finger.FingerPrintProvider;
import cn.zr.aitool.fragments.ContentItemAdapter;
import cn.zr.aitool.fragments.ItemFragment;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, ItemFragment.OnListFragmentInteractionListener {

    private TextView mNavText;
    private MainActivity.SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private String[] mTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentManager.getInstance().setContext(this.getApplicationContext());
        initDb();
        initItemTitle();
        initView();
    }

    private void initDb() {
        DataBaseHelper.getInstance(this);
        NavTextManager.getInstance().copyTextToDb(this);
    }

    private void initItemTitle() {
        mTitles = getResources().getStringArray(R.array.main_item_title);
    }

    private void initView() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        View headerView = navigationView.getHeaderView(0);
        mNavText = headerView.findViewById(R.id.nav_text);
        setmNavText();
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                setmNavText();
                super.onDrawerClosed(drawerView);
            }
        };
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        mSectionsPagerAdapter = new MainActivity.SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }


    private void setmNavText() {
        NavText navText = NavTextManager.getInstance().queryTextById(this);
        mNavText.setText(navText.getText());
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            Intent intent = new Intent(this, FuncitonActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_gallery) {
        } else if (id == R.id.nav_slideshow) {
        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {
        } else if (id == R.id.nav_send) {
            FingerPrintProvider provider = new FingerPrintProvider(this);
            provider.onFingerprintClick();
        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onListFragmentInteraction(ContentItemAdapter.ViewHolder holder) {
        Intent intent;
        switch (holder.mItem.position) {
            case Constants.POSITION_P2P:
                intent = new Intent(this, P2pDetailActivity.class);
                break;
            case Constants.POSITION_JIJIN:
                intent = new Intent(this, JiJinDetailActivity.class);
                break;
            case Constants.POSITION_HOUSE:
                intent = new Intent(this, HouseDetailActivity.class);
                break;
            default:
                intent = new Intent(this, ItemDetailActivity.class);
        }
        intent.putExtra(ContentColumn.content, holder.mItem.content);
        intent.putExtra(ContentColumn.title, holder.mItem.title);
        intent.putExtra(ContentColumn.start, holder.mItem.start);
        intent.putExtra(ContentColumn.end, holder.mItem.end);
        intent.putExtra(ContentColumn.content_one, holder.mItem.content1);
        intent.putExtra(ContentColumn.content_two, holder.mItem.content2);
        intent.putExtra(ContentColumn.content_thr, holder.mItem.content3);
        startActivity(intent);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ItemFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return mTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    @Override
    public void onLongClick(ContentItemAdapter.ViewHolder holder) {
        ContentManager.getInstance().deleteById(holder.mItem.id);
    }
}
